﻿#region Using

using System;
using System.Runtime.Serialization;
using System.Text;

#endregion

namespace Common.Base
{
    [DataContract]
    public class Identifiable
    {
        private static readonly Random Rnd = new Random();

        public override int GetHashCode()
        {
            return this.Identifier;
        }

        protected Identifiable()
        {
            this.Identifier = Rnd.Next();
        }

        protected Identifiable(int id)
        {
            this.Identifier = id;
        }

        [DataMember]
        public int Identifier { get; private set; }

        public override bool Equals(object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return false;
            }

            // If parameter cannot be cast to Point return false.
            var toCompare = obj as Identifiable;
            if (toCompare == null)
            {
                return false;
            }

            // Return true if the fields match:
            return this.Identifier == toCompare.Identifier;
        }

        public Identifiable GenerateIdentifiable()
        {
            return new Identifiable(this.Identifier);
        }

        protected bool Equals(Identifiable other)
        {
            return string.Equals(this.Identifier, other.Identifier);
        }
    }
}